package com.study.demo.data.remote;


import com.stduy.util.Logger;
import com.study.demo.api.forum.ForumApi;
import com.study.demo.data.ContentDataSource;
import com.study.demo.db.ThreadInfo;
import com.study.demo.db.ThreadReply;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Single;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by sll on 2016/6/3.
 */
public class ContentRemoteDataSource implements ContentDataSource {

    private final ForumApi mForumApi;

    @Inject
    public ContentRemoteDataSource(ForumApi mForumApi) {
        this.mForumApi = mForumApi;
    }

    @Override
    public Single<ThreadInfo> getThreadInfo(String fid, String tid) {
        return mForumApi.getThreadInfo(tid, fid, 1, "")
                .onErrorReturn(throwable -> {
                    Logger.e("onErrorReturn: ", throwable);
                    return null;
                })
                .subscribeOn(Schedulers.io());
    }

    @Override
    public Single<List<ThreadReply>> getReplies(String fid, String tid, int page) {
        return mForumApi.getThreadReplyList(tid, fid, page)
                .map(threadReplyData -> {
                    if (threadReplyData != null && threadReplyData.status == 200) {
                        return threadReplyData.result.list;
                    }
                    return null;
                })
                .onErrorReturn(throwable -> null)
                .subscribeOn(Schedulers.io());
    }

    @Override
    public Single<List<ThreadReply>> getLightReplies(String fid, String tid) {
        return mForumApi.getThreadLightReplyList(tid, fid)
                .map(threadLightReplyData -> {
                    if (threadLightReplyData != null && threadLightReplyData.status == 200) {
                        return threadLightReplyData.list;
                    }
                    return null;
                })
                .onErrorReturn(throwable -> {
                    return null;
                })
                .subscribeOn(Schedulers.io());
    }
}
